from django.db import models
from django.contrib.auth.models import User
import django.utils.timezone as timezone
# Create your models here.


class Tag(models.Model):
    tag = models.CharField(max_length=16, null=True, blank=True)

    def __str__(self):
        return self.tag


class Article(models.Model):
    headline = models.CharField(null=True, blank=True, max_length=300)
    content = models.TextField(null=True, blank=True)
    auth = models.ForeignKey(to=User, related_name="write", null=True, blank=True, default=None, on_delete=models.SET)
    tag = models.ManyToManyField(to=Tag, related_name="tag_article", blank=True, default=None)
    like = models.IntegerField(default=0, null=True, blank=True)
    create_time = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return self.headline


class Profile(models.Model):
    user = models.OneToOneField(to=User, related_name="profile", on_delete=models.SET)
    nickname = models.CharField(max_length=128, default="很懒的一个用户")
    phone = models.CharField(max_length=11, default="12112345678")
    address = models.CharField(max_length=256, default="用户很懒，没有填写地址")
    abstract = models.TextField("用户很懒，没有描述")

    def __str__(self):
        return self.nickname


class Issue(models.Model):
    belong_to = models.ForeignKey(to=User, related_name="issues", on_delete=models.SET_DEFAULT, default='匿名')
    title = models.CharField(max_length=256)
    description = models.TextField(null=True, blank=True, default="")
    create_time = models.DateTimeField(null=True, blank=True, default=timezone.now)
    read_time = models.IntegerField(default=0)
    anonymity = models.BooleanField(default=False)

    def __str__(self):
        return self.title


class Answer(models.Model):
    belong_to = models.ForeignKey(to=User, related_name="answers", on_delete=models.SET_DEFAULT, default='匿名')
    issue = models.ForeignKey(to=Issue, related_name="answers", on_delete=models.CASCADE)
    answer = models.TextField(default="")
    create_time = models.DateTimeField(null=True, blank=True, default=timezone.now)
    read_time = models.IntegerField(default=0)
    Status = (
        (0, '草稿'),
        (1, '已发布'),
    )
    status = models.IntegerField(choices=Status, default=0)
    useless = models.BooleanField(default=False)
    agree = models.IntegerField(default=0)
    disagree = models.IntegerField(default=0)
    anonymity = models.BooleanField(default=False)

    def __str__(self):
        return self.answer[:17]


class Attitude(models.Model):
    Attitudes = (
        (-1, '反对'),
        (0, '未表态'),
        (1, '赞同'),
    )
    user = models.ForeignKey(to=User,related_name="user_attitudes", on_delete=models.CASCADE)
    answer = models.ForeignKey(to=Answer,related_name="answer_attitudes", on_delete=models.CASCADE)
    attitude = models.IntegerField(choices=Attitudes,default=0)
    create_time = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return self.attitude


class Comment(models.Model):
    belong_to = models.ForeignKey(to=User, related_name="comments", on_delete=models.CASCADE)
    answer = models.ForeignKey(to=Answer, related_name="comments", on_delete=models.CASCADE)
    content = models.TextField(default="")
    create_time = models.DateTimeField(null=True, blank=True, default=timezone.now)
    reply = models.ForeignKey(to=User, null=True, blank=True, on_delete=models.CASCADE)

    def __str__(self):
        return self.belong_to.username + ' ' + self.content[:28]
